package com.cdu.guoxinan.www.smartcom.controller;

import com.cdu.guoxinan.www.smartcom.domian.pojo.MedicalDoctor;
import com.cdu.guoxinan.www.smartcom.domian.pojo.MedicalServiceConfig;
import com.cdu.guoxinan.www.smartcom.domian.pojo.HealthServiceAppointment;
import com.cdu.guoxinan.www.smartcom.service.MedicalDoctorService;
import com.cdu.guoxinan.www.smartcom.service.MedicalServiceConfigService;
import com.cdu.guoxinan.www.smartcom.service.HealthServiceAppointmentService;
import com.cdu.guoxinan.www.smartcom.unit.model.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/medical")
public class MedicalController {
    @Autowired
    private MedicalDoctorService medicalDoctorService;
    @Autowired
    private MedicalServiceConfigService medicalServiceConfigService;
    @Autowired
    private HealthServiceAppointmentService healthServiceAppointmentService;

    @GetMapping("/doctors")
    public R getAllDoctors() {
        List<MedicalDoctor> doctors = medicalDoctorService.findAll();
        return R.ok(doctors, doctors.size());
    }

    @GetMapping("/services")
    public R getAllServiceConfigs() {
        List<MedicalServiceConfig> services = medicalServiceConfigService.findAll();
        return R.ok(services, services.size());
    }

    @PostMapping("/doctors/add")
    public R addDoctor(@RequestBody MedicalDoctor doctor) {
        boolean success = medicalDoctorService.addDoctor(doctor);
        if (success) {
            return R.ok("添加成功");
        } else {
            return R.error("添加失败");
        }
    }
    @PostMapping("/doctors/update")
    public R updateDoctor(@RequestBody MedicalDoctor doctor) {
        boolean success = medicalDoctorService.updateDoctor(doctor);
        if (success) {
            return R.ok("修改成功");
        } else {
            return R.error("修改失败");
        }
    }
    @PostMapping("/doctors/delete")
    public R deleteDoctor(@RequestParam Long doctorId) {
        boolean success = medicalDoctorService.deleteDoctor(doctorId);
        if (success) {
            return R.ok("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @PostMapping("/services/add")
    public R addServiceConfig(@RequestBody MedicalServiceConfig config) {
        boolean success = medicalServiceConfigService.addServiceConfig(config);
        if (success) {
            return R.ok("添加成功");
        } else {
            return R.error("添加失败");
        }
    }
    @PostMapping("/services/update")
    public R updateServiceConfig(@RequestBody MedicalServiceConfig config) {
        boolean success = medicalServiceConfigService.updateServiceConfig(config);
        if (success) {
            return R.ok("修改成功");
        } else {
            return R.error("修改失败");
        }
    }
    @PostMapping("/services/delete")
    public R deleteServiceConfig(@RequestParam Integer serviceType) {
        boolean success = medicalServiceConfigService.deleteServiceConfig(serviceType);
        if (success) {
            return R.ok("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @PostMapping("/appointment/add")
    public R addAppointment(@RequestBody HealthServiceAppointment appointment) {
        try {
            healthServiceAppointmentService.addAppointment(appointment);
            return R.ok();
        } catch (Exception e) {
            return R.error("预约提交失败: " + e.getMessage());
        }
    }

    @GetMapping("/appointment/list")
    public R listAppointments(@RequestParam(defaultValue = "1") int page,
                              @RequestParam(defaultValue = "10") int limit,
                              @RequestParam(required = false) Integer serviceType,
                              @RequestParam(required = false) Integer status,
                              @RequestParam(required = false) String appointmentDate) {
        List<HealthServiceAppointment> data = healthServiceAppointmentService.getAppointmentsByCondition(serviceType, status, appointmentDate, page, limit);
        int count = healthServiceAppointmentService.getTotalCountByCondition(serviceType, status, appointmentDate);
        return R.ok(data, count);
    }

    @GetMapping("/appointment/my-list")
    public R listMyAppointments(@RequestParam(defaultValue = "1") int page,
                                @RequestParam(defaultValue = "10") int limit,
                                @RequestParam Long residentId,
                                @RequestParam(required = false) Integer serviceType,
                                @RequestParam(required = false) Integer status,
                                @RequestParam(required = false) String appointmentDate) {
        List<HealthServiceAppointment> data = healthServiceAppointmentService.getAppointmentsByResidentId(residentId, serviceType, status, appointmentDate, page, limit);
        int count = healthServiceAppointmentService.getTotalCountByResidentId(residentId, serviceType, status, appointmentDate);
        return R.ok(data, count);
    }

    @PostMapping("/appointment/cancel")
    public R cancelAppointment(@RequestParam Long appointmentId) {
        boolean success = healthServiceAppointmentService.cancelAppointment(appointmentId);
        if (success) {
            return R.ok("取消成功");
        } else {
            return R.error("取消失败");
        }
    }

    @PostMapping("/appointment/delete")
    public R deleteAppointment(@RequestParam Long appointmentId) {
        boolean success = healthServiceAppointmentService.deleteAppointment(appointmentId);
        if (success) {
            return R.ok("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @PostMapping("/appointment/finish")
    public R finishAppointment(@RequestParam Long appointmentId) {
        boolean success = healthServiceAppointmentService.finishAppointment(appointmentId);
        if (success) {
            return R.ok("已标记为完成");
        } else {
            return R.error("操作失败");
        }
    }
} 